home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 038a / bas_int1.zip / COPY.BAS < prev    next >
BASIC Source File  |  1991-06-12  |  5KB  |  120 lines

  1. '=========================================================================
  2. 'Routine Type: FUNCTION
  3. '        Name: COPY.BAS by Dave Cleary
  4. '     Purpose: To copy a file without using SHELL "COPY..."
  5. ' Call Syntax: CopyFile% (Source$, Dest$)
  6. '     Returns: 0 - Everything went all right
  7. '              1 - Source file does not exist
  8. '              2 - Destination file does exist
  9. '              3 - Failure in setting copy's time/date to that of source
  10. '   Libraries: QB.QLB (QB.LIB) for CALL INTERRUPT
  11. 'External Routines Required: DIR$ function with QuickBASIC v4.x
  12. '                            also written by Dave Cleary
  13. '
  14. '-----------------------------------------------------------------------
  15. 'Background Information:
  16. '
  17. '   QuickBASIC has many different ways to read an write files. Copying
  18. 'files is easily done in QuickBASIC and is quite fast also.
  19. '
  20. '   Lets first look at the options QB gives us to read a file. The three
  21. 'major commands to read a file are LINE INPUT, INPUT$, and GET. LINE
  22. 'INPUT is only good for text files and is also one of the slowest QB
  23. 'commands as far as file IO goes. That leaves INPUT$ and GET.
  24. '
  25. '   GET and PUT are by far the fastest IO commands QB has. This is
  26. 'because with GET, the area of memory where the data gets placed is
  27. 'already allocated. All QB has to do is set up some registers and then
  28. 'call DOS to read the file.
  29. '
  30. '   INPUT$ is another command that works for both text and binary files.
  31. 'INPUT$ is slightly slower than the GET command because it allocates
  32. 'memory to hold the data each time it is called. I have found this speed
  33. 'difference to be inconsequential though. INPUT$ does offer some
  34. 'advantages over GET in our CopyFile routine. When you are at the end of
  35. 'a file, INPUT$ will return a string with the exact number of bytes left
  36. 'in the file even if you asked for more. This relieves us of knowing the
  37. 'size of the file we are working with. GET, on the other hand, will pad
  38. 'your buffer with null characters, causing us to have to truncate the
  39. 'last read if we want to keep our file lengths the same. For this reason,
  40. 'I chose INPUT$ over GET to read in the file.
  41. '
  42. '========================================================================
  43. '
  44. 'So here is our copy file routine:
  45. 'Main()
  46.      DEFINT A-Z 
  47.      
  48.      DECLARE FUNCTION DIR$ (FileSpec$)      'Comment out if using BASIC 7
  49.      DECLARE FUNCTION CopyFile% (Source$, Dest$)
  50.      
  51.      '$INCLUDE: 'QB.BI'                     'Required for CALL INTERRUPT
  52.      
  53.      CONST Block = 4096                     'Set this to the length you
  54.                                             'want your buffer to be
  55.      
  56.      'Example of how to call CopyFile
  57.      'Ercd = CopyFile("D:\PDQ\HISTORY.DOC", "C:\SCRAP\TEST1")
  58.      
  59.  FUNCTION CopyFile% (Source$, Dest$) STATIC
  60.      
  61.      DIM Regs AS RegType                    'Needed for CALL INTERRUPT
  62.      
  63.      '-----  See if source file exists
  64.      IF LEN(DIR$(Source$)) = 0 THEN         'Use my DIR$ function
  65.                                             'if you are
  66.                                             'using QB 4.x.
  67.         CopyFile% = 1                       'Source doesn't exist 
  68.         EXIT FUNCTION                       'Exit with error code
  69.      END IF
  70.      
  71.      '-----  See if destination exists
  72.      IF LEN(DIR$(Dest$)) THEN
  73.         CopyFile% = 2                       'Destination already exists 
  74.         EXIT FUNCTION                       'Exit with error code
  75.      END IF
  76.      
  77.      '-----  Open files for BINARY
  78.      SFileNum = FREEFILE
  79.      OPEN Source$ FOR BINARY AS #SFileNum
  80.      
  81.      DFileNum = FREEFILE
  82.      OPEN Dest$ FOR BINARY AS #DFileNum
  83.      
  84.      '-----  Now copy the files over
  85.      DO
  86.         Buffer$ = INPUT$(Block, #SFileNum) 
  87.         PUT #DFileNum, , Buffer$
  88.      LOOP UNTIL EOF(SFileNum)
  89.      
  90.      '-----  Set the date and time of the copy to that of the original
  91.      Regs.ax = &H5700
  92.      Regs.bx = FILEATTR(SFileNum, 2)        'This gets DOS's file handle
  93.      INTERRUPT &H21, Regs, Regs             'Get date and time of original
  94.      
  95.      '-----  Check for an error
  96.      IF (Regs.flags AND 1) THEN
  97.         CLOSE #SFileNum, #DFileNum          'Close the files 
  98.         KILL Dest$                         'Kill our copy because something 
  99.         CopyFile% = 3                       'went wrong. Exit with error 
  100.         EXIT FUNCTION
  101.      END IF
  102.      
  103.      Regs.ax = &H5701
  104.      Regs.bx = FILEATTR(DFileNum, 2)
  105.      INTERRUPT &H21, Regs, Regs             'Set date and time of copy
  106.      
  107.      '-----  Check for an error
  108.      IF (Regs.flags AND 1) THEN
  109.         CLOSE #SFileNum, #DFileNum          'Close the files 
  110.         KILL Dest$                         'Kill our copy because something 
  111.         CopyFile% = 3                       'went wrong. Exit with error 
  112.         EXIT FUNCTION
  113.      END IF
  114.      
  115.      CLOSE #SFileNum, #DFileNum             'All done
  116.      CopyFile% = 0                          'Return with success
  117.      
  118.  END FUNCTION
  119.  
  120.